package com.yzy.config;

import com.yzy.vo.TestVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Properties;

/**
 * @author yangzhenyu
 * @version 1.0
 * @description:
 * @date 2022/7/7 14:56
 */
@Configuration
public class TestConfig {
    @Autowired
    Environment environment;
    @Bean(name = "bTestVo")
    public TestVo bTestVo(){
        Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources.get(environment);
        Binder binder = new Binder(sources);
        BindResult<Properties> bindResult = binder.bind("spring.thymeleaf",Properties.class);
        Properties properties = bindResult.get();


        TestVo testVo = new TestVo();
        testVo.setName("yangzhenyu");
        return testVo;
    }

        //Bean的初始化回调方法和释放资源的回调方法
    @Bean(name = "testVo",initMethod = "init",destroyMethod = "destroy")
    public TestVo testVo(@Qualifier("bTestVo") TestVo testVo){
        //获取TestVo方法的getName方法
        Method getName= ReflectionUtils.findMethod(TestVo.class, "getName");
        //指定对象中执行方法
        Object o = ReflectionUtils.invokeMethod(getName, testVo);
        String format = MessageFormat.format("{0}执行bean成功!!!", o);
        System.out.println(format);
        return testVo;
    }

    public static void main(String[] args) {
        String s = "2";
        String i = "";
        if (StringUtils.isNotBlank(s)&&StringUtils.isNotBlank(i)){
            System.out.printf("YES");
        }
    }



}
